<!--
 @description 用户组织关系配置(账号与经销商关联关系配置)
 @fileName index.vue
 @author zengqiongying
 @created 2024/09/24 13:55:33
-->
<template>
  <div>
    <search-form
      :search-config="searchConfig"
      :label-width="90"
      @search="handleSearch"
      @visible-change="resetTableHeight"
    />
    <table-box
      ref="refTable"
      :selected-arry.sync="tableSelected"
      :table-config="tableConfig"
      :other-filters="otherFilters"
      :off-set="-10"
      table-row-key="id"
      show-index
      :show-custom-collect="false"
      :show-custom-combine="false"
      :show-custom-setting="false"
      :setting-auto-request="false"
      @btnOperate="btnOperate"
    />
    <edit
      v-if="showEdit"
      :visible.sync="showEdit"
      :detail="activeRowData"
      @success="refreshTable"
    />
  </div>
</template>

<script>
import { parseTime } from '@/utils'
import { delConfig } from '@/api/accountCustomerConfig'
import Edit from './Edit.vue'
export default {
  name: 'AccountCustomerConfig',
  components: { Edit },
  data() {
    return {
      searchConfig: [
        { label: '用户姓名', prop: 'userName', type: 'text' },
        { label: '用户名', prop: 'account', type: 'text' },
        { label: '员工编号', prop: 'staffCode', type: 'text' },
        { label: '电话', prop: 'phone', type: 'text' },
        { label: '创建时间', prop: 'createTime', type: 'date', value: '', dateType: 'daterange' },
        { label: '创建人', prop: 'createBy', type: 'text' },
        { label: '修改时间', prop: 'lastUpdateTime', type: 'date', value: '', dateType: 'daterange' },
        { label: '修改人', prop: 'lastUpdateBy', type: 'text' }
      ],
      showEdit: false, // 是否编辑基础信息
      activeRowData: '', // 当前选中行数据
      tableSelected: [], // 当前所选中表格行数据
      otherFilters: [], // 表格额外查询条件
      otherRaws: {},
      tableConfig: {
        interfacePath: `${process.env.VUE_APP_BASE_API}/api/v1/accountCustomerConfig/page`,
        hideSearchBar: true,
        pageSize: 20,
        sort: 'createTime',
        order: 'asc',
        setting: [
          {
            prop: 'userName',
            label: '用户姓名',
            minWidth: 150
          },
          {
            prop: 'account',
            label: '用户名',
            minWidth: 150
          },
          {
            prop: 'staffCode',
            label: '员工编号',
            minWidth: 150
          },
          {
            prop: 'phone',
            label: '电话',
            minWidth: 150
          },
          {
            prop: 'customerName',
            label: '组织名称',
            minWidth: 150
          },
          {
            prop: 'customerCode',
            label: '组织编码',
            minWidth: 150
          },
          {
            prop: 'createBy',
            label: '创建人',
            minWidth: 150,
            type: 'hidden'
          },
          {
            prop: 'createTime',
            label: '创建时间',
            minWidth: 150,
            type: 'hidden',
            formatData: ({ createTime }) => {
              return parseTime(createTime)
            }
          },
          {
            prop: 'lastUpdateBy',
            label: '修改人',
            minWidth: 150,
            type: 'hidden'
          },
          {
            prop: 'lastUpdateTime',
            label: '修改时间',
            minWidth: 150,
            type: 'hidden',
            formatData: ({ lastUpdateTime }) => {
              return parseTime(lastUpdateTime)
            }
          }],
        btnList: !this.isProduction ? [
          {
            buttonName: '新增',
            optCode: 'add',
            outerFlag: true
          },
          {
            buttonName: '编辑',
            optCode: 'edit',
            outerFlag: true
          },
          {
            buttonName: '删除',
            optCode: 'del',
            outerFlag: true
          }] : null
      }
    }
  },

  computed: {},

  created() {},
  activated() {},

  mounted() {},

  methods: {
    btnOperate(code) {
      switch (code) {
        case 'add':
          this.handleEdit()
          break
        case 'edit':
          if (!this.singleJudge(this.tableSelected, '编辑', true)) {
            return
          }
          this.handleEdit(this.tableSelected[0])
          break
        case 'del':
          if (!this.singleJudge(this.tableSelected, '删除', true)) {
            return
          }
          this.handleDelete()
          break
        default:
          break
      }
    },
    // 刷新表格数据
    refreshTable(isInit) {
      this.$nextTick(() => {
        this.$refs.refTable.initTableData(isInit ? '' : 'onPage')
      })
    },
    clearSearch() {

    },
    resetTableHeight() {
      this.$refs.refTable.refreshTableHeight()
    },
    // 搜索
    handleSearch(params) {
      this.otherFilters = params.filtersRaw || []
      this.otherRaws = params.paramsRaw || {}
      this.refreshTable(true)
    },
    // 新增/编辑基础信息
    handleEdit(row) {
      this.activeRowData = row && row.id ? row : ''
      this.$nextTick(() => {
        this.showEdit = true
      })
    },
    // 删除处理
    handleDelete() {
      const { id } = this.tableSelected[0]
      this.$confirm(`此操作将删除当前所选用户与组织的关联关系, 是否继续?`, '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delConfig(id).then(res => {
          if (res.success === 1) {
            this.$message({
              message: res.msg || '删除成功',
              type: 'success'
            })
            this.refreshTable()
          }
        })
      }).catch(() => {})
    }
  }
}

</script>
<style lang='scss' scoped>
</style>
